package com.lyw.camera

import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.core.app.ActivityCompat
import java.io.File

object PreviewManager {


   private val previewCallbacks = mutableListOf<PreviewCallback>()

    fun registerCallback(callback:PreviewCallback){
        if (!previewCallbacks.contains(callback)){
            previewCallbacks.add(callback)
        }

    }

    fun unRegisterCallback(callback:PreviewCallback){
        previewCallbacks.remove(callback)
    }

    internal fun callback(filePath:String){
        previewCallbacks.forEach {
            it.takePicture(filePath)
        }
    }
    fun start(context: Context, file: File?=null){
        var intent = Intent(context,PreviewActivity::class.java);
        if (intent !is Activity){
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        }
        intent.putExtra(PreviewActivity.KEY_FILE_PATH,file?.absolutePath?:"")
        ActivityCompat.startActivity(context,intent,null)
    }
}